package ch.fusun.baron.printing.service.impl;

import java.util.LinkedList;
import java.util.List;

import ch.fusun.baron.core.rmi.User;
import ch.fusun.baron.data.AbstractDataProvider;
import ch.fusun.baron.data.DataUpdate;
import ch.fusun.baron.printing.MessagingService;
import ch.fusun.baron.printing.MessagingServiceDataUpdate;

/**
 * Implementation of the messaging service
 */
public class MessagingServiceImpl extends AbstractDataProvider implements
		MessagingService {

	private final List<GameMessage> messageBuffer = new LinkedList<GameMessage>();

	/**
	 * Injection constructor
	 */
	public MessagingServiceImpl() {
	}

	@Override
	public DataUpdate createFullUpdate() {
		List<GameMessage> copy = new LinkedList<GameMessage>();
		copy.addAll(messageBuffer);
		MessagingServiceDataUpdate update = new MessagingServiceDataUpdate(copy);
		messageBuffer.clear();
		return update;
	}

	@Override
	public void addMessages(List<GameMessage> messages) {
		messageBuffer.clear();
		messageBuffer.addAll(messages);
		updateAllListeners(new AddMessagesDataUpdate(messages));
	}

	@Override
	public void addMessage(User user, String message) {
		messageBuffer.clear();
		messageBuffer.add(new GameMessage(user, message));
		updateAllListeners(new AddMessageDataUpdate(user, message));
	}

	@Override
	public List<GameMessage> getMessages() {
		return messageBuffer;
	}

}
